package com.taomu.app.zongchouhuli.weight.popwindow;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.weight.popwindow.adpter.SingleSelectAdp;

import java.util.List;

public class PopSelectAccountManager implements CommonPopupWindow.ViewInterface {

    private CommonPopupWindow mCommonPop;

    private RecyclerView mAccountRecy;

    private SingleSelectAdp mAdp;

    private Context mContext;

    public PopSelectAccountManager(Context mContext){
        this.mContext = mContext;
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        initView(view);
        initEvent();
    }

    private void initView(View view){
        mAdp = new SingleSelectAdp(mContext);
        mAccountRecy = view.findViewById(R.id.pop_selectaccount_recy);
        mAccountRecy.setLayoutManager(new LinearLayoutManager(mContext));
        mAccountRecy.setAdapter(mAdp);
        mAdp.setData(mData);
    }

    private void initEvent(){
        mAdp.setSingleAdpListener(new SingleSelectAdp.SingleAdpListener() {
            @Override
            public void onItemClick(String entity) {
                if(listener != null){
                    listener.onItemClick(entity);
                }
                if(mCommonPop != null){
                    mCommonPop.dismiss();
                }
            }
        });
    }

    private List<String> mData;

    public void setData(List<String> mData){
        this.mData = mData;
        if(mAdp != null){
            mAdp.setData(mData);
        }
    }

    public void showPopWindow(View view){
        if (mCommonPop != null && mCommonPop.isShowing()) {
            return;
        } else {
            if (mCommonPop != null) {

            } else {
                mCommonPop = new CommonPopupWindow.Builder(mContext)
                        .setView(R.layout.pop_selectaccount)
                        .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setAnimationStyle(R.style.AnimDown)
                        .setViewOnclickListener(this)
                        .setOutsideTouchable(true)
                        .create();
            }
            mCommonPop.showAsDropDown(view);
        }
    }

    private PopSelectAccountListener listener;

    public void setPopSelectAccountListener(PopSelectAccountListener listener){
        this.listener = listener;
    }

    public interface PopSelectAccountListener{
        void onItemClick(String entity);
    }
}
